<template>
  <div>
    <div class="item">
      <div class="top">
        <p class="top-title">{{ tiezi.forumTitle }}</p>
      </div>
      <div :class="isOpen? 'new_detail' : 'default'" ref="infoItem">{{ tiezi.forumContent }}</div>
      <el-button type="text" class="popper-btn" @click="isOpen=!isOpen" v-if="hasMoreArrow">
        {{word}}
        <i :class="isOpen?'el-icon-arrow-up':'el-icon-arrow-down'"></i>
      </el-button>
      <div class="bottom">
        <div class="time">
          <p style="margin-left:15px">发帖人：{{ tiezi.createBy }}</p>
          <el-tag
            size="mini"
            type="danger"
            v-show="tiezi.createUserId"
            style="margin-left: -25px;"
          >作者</el-tag>
          <p style="margin-left: 15px;">{{ tiezi.createTime }}</p>
        </div>
        <el-button
          type="danger"
          plain
          class="delete-item"
          @click="deleteTie(tiezi)"
          v-if="tiezi.createUserId==userId?true:false"
        >删除</el-button>
      </div>
    </div>
    <el-row class="touxiang">
      <el-avatar shape="circle" :size="50" :src="images"></el-avatar>
      <el-input
        @input="shuru(content)"
        v-model="content"
        style="margin-top:0.5%;margin-left:1%;width:90%"
        :placeholder="who"
      ></el-input>
      <el-button
        :disabled="disabled"
        type="primary"
        class="delete-item"
        style="margin-top:0.6%;margin-left:1%"
        @click="submitForm2"
      >发送</el-button>
    </el-row>
    <!-- 评论内容 -->
    <div class="pinlunContent">
      <div class="top">
        <p>{{total}}条评论</p>
      </div>
      <div class="bottom">
        <ul v-for="(item, index) in xmForumCommentLists" :key="index">
          <el-row style="display:flex;align-items:center">
            <el-avatar shape="circle" :size="32" :src="item.avatar"></el-avatar>
            <div style="color:#454545;font-weight:700;margin-left:1%">{{ item.createBy }}</div>
            <el-tag
              size="mini"
              type="danger"
              v-show="tiezi.createUserId==item.createUserId?true:false"
              style="margin-left: 5px;"
            >作者</el-tag>
            <el-tag
              size="mini"
              type="danger"
              v-show="item.isEnter==1?true:false"
              style="margin-left: 5px;border:none"
            >想加入</el-tag>
          </el-row>
          <el-row style="margin-top:0.6%;padding-left:3.5%">
            <div style="color:#333333;">{{ item.commentContent }}</div>
            <div class="time1">
              <p style="font-size:12px">{{ item.createTime }}</p>
              <el-button class="num" @click="openLund(item)">
                <div style="margin-bottom:6px">
                  <i class="el-icon-chat-dot-square" style="margin-right: 0px"></i>
                  评论
                </div>
              </el-button>
              <el-button
                class="num1"
                v-if="item.createUserId==userId?true:false"
                @click="deletePing(item)"
              >
                <div style="margin-bottom:6px">
                  <i class="el-icon-delete" style="margin-right: 0px"></i>
                  删除
                </div>
              </el-button>
            </div>
          </el-row>
          <li class="replay-box" style="display: block;padding-left:3%">
            <div class="comment-line-box" v-for="items in item.children">
              <el-row style="display:flex;align-items:center">
                <el-avatar shape="circle" :size="32" :src="items.avatar"></el-avatar>
                <div style="color:#454545;margin-left:1%">{{ items.createBy }}</div>
                <el-tag
                  size="mini"
                  type="danger"
                  v-show="items.createUserId==tiezi.createUserId?true:false"
                  style="margin-left: 5px;"
                >作者</el-tag>
                <el-tag
                  size="mini"
                  type="danger"
                  v-show="items.isEnter==1?true:false"
                  style="margin-left: 5px;"
                >想加入</el-tag>
              </el-row>
              <el-row style="margin-top:0.6%;padding-left:3.5%">
                <div style="margin-left:0.4%">{{ items.commentContent }}</div>
                <div class="time1">
                  <p style="font-size:12px;margin-left:0.5%">{{ items.createTime }}</p>
                  <el-button
                    class="num1"
                    v-if="items.createUserId==userId?true:false"
                    @click="deletePing(items)"
                  >
                    <div style="margin-bottom:6px">
                      <i class="el-icon-delete" style="margin-right: 0px"></i>
                      删除
                    </div>
                  </el-button>
                </div>
              </el-row>
            </div>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>
  
  <script>
import { websock } from "@/websock";
import {
  getUserId,
  getforumId,
  saveCommen,
  userOneStep,
  deleteTieZi,
  deletePingLun
} from "@/api2/luntan";
export default {
  props: {
    pinglunList: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      userName: "",
      images: "",
      disabled: true,
      hasMoreArrow: "",
      userPeople: {}, //作者信息
      isOpen: false, //默认折叠
      dialogVisible: false,
      opens: false,
      pingLun: false,
      content: "",
      childComments: [],
      parentComment: {},
      rootParentId: "",
      userId: "",
      pid: "",
      total: "",
      who: "请输入",
      wenzhang: [],
      forumCommentIds: "",
      xmForumCommentLists: [],
      tiezi: this.pinglunList
    };
  },
  created() {
    this.getUsers();
    this.getforumlist(this.tiezi.forumId);
    websock.getUserInfo().then(data => {
      console.log(data);
      this.userPeople = data;
    });
  },
  computed: {
    // 内容
    word() {
      if (this.isOpen === false) {
        return "展开";
      } else if (this.isOpen === true) {
        return "收起";
      } else if (this.isOpen === "") {
        return null;
      }
    }
  },
  mounted() {
    let cellChild = this.$refs.infoItem;
    this.hasMoreArrow = cellChild.scrollHeight > cellChild.offsetHeight;
  },
  methods: {
    fatherMethod(e) {
      this.getforumlist(this.tiezi.forumId);
    },
    deleteTie(e) {
      this.forumIds = e.forumId;
      this.$confirm("确认删除吗, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(res1 => {
          const forumId = e.forumId;
          deleteTieZi(forumId).then(res => {
            if (res.code == 0) {
              this.$message.success("删除成功");
              // this.getUsers();
              this.$emit("fatherMethod");
              // this.getList(this.shenPi); //获取表格数据
            } else {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
      // formLabelAlign: { num: "", yijian: "", yuanyin: "" }
    },
    deletePing(e) {
      console.log(e);
      this.$confirm("确认删除吗, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(res1 => {
          const commentId = e.forumCommentId;
          deletePingLun(commentId).then(res => {
            if (res.code == 0) {
              this.$message.success("删除成功");
              // this.getUsers();
              this.getforumlist(this.tiezi.forumId);
              //   this.$emit("fatherMethod");
              // this.getList(this.shenPi); //获取表格数据
            } else {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
      // formLabelAlign: { num: "", yijian: "", yuanyin: "" }
    },
    openLund(e) {
      console.log(e);
      this.who = `回复： ${e.createBy}`;
      this.forumCommentIds = e.forumCommentId;
      this.userName = e.createBy;
      this.childComments = e.children;
    },
    //
    shuru(e) {
      if (e !== "") {
        this.disabled = false;
      } else {
        this.disabled = true;
      }
    },
    // pinglun
    async submitForm2() {
      if (this.forumCommentIds == "") {
        const xmForumComment = {
          forumId: this.tiezi.forumId,
          commentContent: this.content
        };
        const res = await saveCommen(xmForumComment);
        if (res.code == 0) {
          this.$message.success("评论成功");
          this.content = "";
          this.getUsers();
          this.disabled = true;
          this.getforumlist(this.tiezi.forumId);
        }
      } else {
        const xmForumComment = {
          // openId: this.opensid,
          pid: this.forumCommentIds,
          forumId: this.tiezi.forumId,
          commentContent: this.content
        };
        const res = await saveCommen(xmForumComment);
        if (res.code == 0) {
          this.$message.success("评论成功");
          this.content = "";
          this.disabled = true;
          this.getUsers();
          this.getforumlist(this.tiezi.forumId);
          // this.getList(this.shenPi); //获取表格
        }
      }
    },
    async getforumlist(e) {
      const res = await getforumId(e);
      this.xmForumCommentLists = res.data.xmForumCommentList.reverse();
      console.log(this.xmForumCommentLists);
      this.total = res.data.xmForumCommentList.length;
    },
    async getUsers() {
      const res = await getUserId();
      console.log(res);
      this.userId = res.data.id;
      const res1 = await userOneStep();
      res1.data.userList.forEach(element => {
        if (element.id == this.userId) {
          console.log(element);
          this.images = element.avatar;
        }
      });
    }
  }
};
</script>
  <style lang="scss" scoped>
.item {
  position: relative;
  //   border-bottom: 1px solid #ccc;
  .top {
    display: flex;
    .top-title {
      margin-top: -1%;
      font-size: 18px;
      margin-right: 20px;
      font-weight: bold;
    }
  }
  .center {
    font-size: 16px;
    color: #454545;
    width: 90%;
    word-break: break-all;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2; /* 超出几行省略 */
    overflow: hidden;
  }
  .bottom {
    font-size: 14px;
    margin: 10px 10px 10px 0px;
    color: #aaa;
    display: flex;
    p {
      margin-right: 30px;
      a {
        text-decoration: underline;
      }
    }
  }
}
.time {
  min-width: 38vh;
  height: 36px;
  display: flex;
  background: #f5f5f5;
  border-radius: 4px 4px 4px 4px;
  opacity: 1;
  color: #666666;
  text-align: center;
  align-items: center;
  line-height: 10px;
  margin-right: 10px;
}
.time1 {
  width: 32%;
  height: 36px;
  display: flex;
  border-radius: 4px 4px 4px 4px;
  opacity: 1;
  color: #666666;
  text-align: center;
  align-items: center;
  line-height: 10px;
  margin-right: 10px;
}
.num {
  margin-top: 1px;
  margin-right: -6%;
  height: 36px;
  line-height: 10px;
  align-items: center;
  background-color: #fff;
  border: none;
  color: #3b95ff;
}
.num1 {
  margin-top: 1px;
  margin-left: 1%;
  height: 36px;
  line-height: 10px;
  align-items: center;
  background-color: #fff;
  border: none;
  color: red;
}
.num :hover {
  color: rgb(81, 0, 255);
}
.num1 :hover {
  color: #ccc;
}
.delete-item {
  height: 36px;
  opacity: 0.8;
}
// css
.new_detail {
  font-size: 16px;
  color: #454545;
  width: 90%;
}
.default {
  font-size: 16px;
  color: #454545;
  width: 90%;
  word-break: break-all;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2; /* 超出几行省略 */
  overflow: hidden;
}
.popper-btn {
  position: absolute;
  right: 55px;
  bottom: 33px;
}
.touxiang {
  margin-top: 1%;
  display: flex;
  align-content: center;
  justify-content: space-evenly;
}
.pinlunContent {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  font-size: 14px;
  margin-top: 1.5%;
  border: 1px solid #ccc;
  height: 570px;
  .top {
    width: 95%;
    height: 30px;
    font-size: 14px;
    margin-bottom: 1%;
    border-bottom: 1px solid #ccc;
    line-height: 0px;
  }
  .bottom {
    width: 100%;
    height: 500px;
    overflow: auto;
  }
  .bottom::-webkit-scrollbar {
    display: none;
  }
}
</style>
  